@echo off

REM Usage:
REM         lp_main
REM 
REM Description:
REM         a simple game called little person
REM 
REM #############################################################
REM initializing...
VERIFY OTHER 2>nul
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
IF ERRORLEVEL 1 echo Unable to enable extensions
color 0A
REM mode con cols=53 lines=51
mode con cols=53 lines=46
REM #############################################################


:PRESTART
REM =============================================================
cls
if EXIST lpgameover.log (
for /l %%a in (1,1,10) do @echo.
echo ================================================
echo.
echo  Last game:
for /f "tokens=1*" %%i in (lpgameover.log) do @echo  %%i%%j
echo.
echo.
echo ================================================
echo press any key to restart a new game!
pause>nul
del /q lpgameover.log
) else (
call disp.bat START
pause
)


set lpside=R
set event=0
set stepcounter=0
set mcounter=0
REM the last position
set lastpos=R
REM the last last position
set llastpos=R
set serialpos=#
set nextplat=L

REM initialize the interface
call disp.bat INIT
set /p IsOK=press enter to start...
cls
call disp.bat INIT
REM =============================================================

:MAINCONTROL

call disp.bat !event!$!lpside!$!mcounter!$!stepcounter!$!lastpos!$!nextplat!

if EXIST lpgameover.log goto PRESTART
set event=0
set comple=0
set /a comple=!stepcounter!%%9
if "!comple!" == "0" (
    if "!nextplat!" == "L" ( set nextplat=R) else ( set nextplat=L)
)

set /a stepcounter+=1
set llastpos=!lastpos!
set lastpos=!lpside!
set serialpos=#!lpside!!serialpos!

for /f %%a in ('choice /N /C LRX /T 1 /D X') do @if "%%a" == "X" (
REM echo this is X
if "!lpside!" == "M" set /a mcounter+=1
) else (
    set event=1
    if "%%a" == "L" (
        if "!lpside!" == "R" (
            set /a mcounter+=1
            set lpside=M
            goto MAINCONTROL
        )
        if "!lpside!" == "M" (
            if "!llastpos!" == "L" ( set /a mcounter+=1)
            if "!llastpos!" == "M" (
            for /f "tokens=1,2,3* delims=#" %%A in ("!serialpos!") do @if "%%C" == "R" ( set /a mcounter+=1)
            )
            set lpside=L
            goto MAINCONTROL
        )
    ) else (
        if "!lpside!" == "L" (
            set /a mcounter+=1
            set lpside=M
            goto MAINCONTROL
        )
        if "!lpside!" == "M" (
            if "!llastpos!" == "R" ( set /a mcounter+=1)
            if "!llastpos!" == "M" (
            for /f "tokens=1,2,3* delims=#" %%A in ("!serialpos!") do @if "%%C" == "L" ( set /a mcounter+=1)
            )
            set lpside=R
            goto MAINCONTROL
        )
    )
    set event=0
)
goto MAINCONTROL

:EOF
exit 0
